package com.example.a2209_shop.search

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.TextView
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.a2209_shop.R
import com.example.a2209_shop.adapter.GoodsAdapter
import com.example.a2209_shop.adapter.GoodsMutliAdapter
import com.example.a2209_shop.database.DataBaseUtils
import com.example.a2209_shop.databinding.ActivitySearchBinding
import com.example.base.entity.ChipEntity
import com.example.base.mvvm.BaseActivity

@Route(path = "/app/SearchActivity")
class SearchActivity : BaseActivity<ActivitySearchBinding,SearchViewModel>() {
    lateinit var goodsAdapter: GoodsMutliAdapter
    override fun initData() {
        viewModel.searchSuccess.observe(this){
            goodsAdapter.data.clear()
            if(it.size>0){
                it.forEachIndexed { index, goodsEntity ->
                    goodsEntity.type = index%2//循环给type赋值
                }
                goodsAdapter.data.addAll(it)
            }else{
                ToastUtils.showLong("未查找到相关数据")
            }
            goodsAdapter.notifyDataSetChanged()
        }
    }

    @SuppressLint("MissingInflatedId")
    override fun initView() {
        goodsAdapter = GoodsMutliAdapter()
        binding.rvResult.adapter = goodsAdapter
        binding.rvResult.layoutManager = GridLayoutManager(this,2)


        binding.sv.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                //点击回车键后查询
                if(query!=null){
                    viewModel.search(query)
                    //新增历史搜索记录
                    DataBaseUtils.getDb().getChipDao().insertChip(ChipEntity(query))
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                //实时搜索
                if(newText!=null){
                    viewModel.search(newText)
                }
                return false
            }
        })
        //展示历史搜索记录
        val data = DataBaseUtils.getDb().getChipDao().queryChip()
        if(data.isNotEmpty()){
            binding.cg.removeAllViews()//先清空之前的历史记录
            data.forEach {
                val key = it.key
                val view = LayoutInflater.from(this).inflate(R.layout.item_cg,null)
                view.findViewById<TextView>(R.id.chip).text = key
                binding.cg.addView(view)//历史记录出来了
                //点击实现查询
                view.setOnClickListener {
                    binding.sv.setQuery(key,true)
                }
            }
        }
    }

    override fun getViewModel() = SearchViewModel::class.java

    override fun getLayout() = R.layout.activity_search

}